local skel = fk.CreateSkill {
  name = "emo__pod",
}

Fk:loadTranslationTable{
  ["emo__pod"] = "Pod",
  [":emo__pod"] = "每轮开始时，你获得三张同类型的牌，每当你受到伤害后，你可以弃置一张以此法获得的牌，若为：装备牌，令一名角色翻面；锦囊牌，获得一个此时机的技能；基本牌，你下次造成伤害后执行之前的一项。此轮结束时，你弃置以此法获得的牌，以获得一个额外回合。",

  ["@@emo__pod-inhand"] = "Pod",
  --["@@emo__pod-turn"] = "Pod:封印",
  ["@emo__pod_damage"] = "Pod:造伤",
  ["#emo__pod-cost"] = "Pod：你可以弃置一张Pod牌，按照类型执行效果",
  ["#emo__pod-turnover"] = "Pod：令一名角色翻面",
  ["#emo__pod-damage"] = "Pod：选择一名角色，令其翻面；若不选，你获得一个技能",

  ["$emo__pod1"] = "援护就交给你了",
  ["$emo__pod2"] = "Pod",
}

skel:addEffect(fk.RoundStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local pile = table.simpleClone(room.draw_pile)
    local search_discard = false
    local cards = {}
    local map = {}
    -- 搜索逻辑：随机搜牌，直到任意类型先满3张，这样搜出基本概率比较高
    while #pile > 0 do
      local id = table.remove(pile, math.random(#pile))
      local t = Fk:getCardById(id).type
      if map[t] == nil then
        map[t] = {id}
      else
        table.insert(map[t], id)
        if #map[t] == 3 then
          cards = map[t]
          break
        end
      end
      if #pile == 0 then
        if search_discard then break end
        search_discard = true
        pile = table.simpleClone(room.discard_pile)
      end
    end
    if #cards == 0 then
      if next(map) == nil then return end
      for _, ids in pairs(map) do
        cards = ids
        break
      end
    end
    room:setPlayerMark(player, "emo__pod-round", cards)
    room:obtainCard(player, cards, false, fk.ReasonPrey, player, skel.name, "@@emo__pod-inhand")
  end,
})

--- 获得一个此时机的技能
---@param player ServerPlayer
local function getSkillFromEvent(player, event)
  local room = player.room
  local tag = room:getTag("emo__pod_"..event)
  local map = { ["Damaged"] = fk.Damaged, ["Damage"] = fk.Damage }
  local event_class = map[event]
  if event_class == nil then return end
  if tag == nil then
    tag = {}
    for _, g in pairs(room.general_pile) do
      local general = Fk.generals[g]
      for _, s in pairs(general:getSkillNameList()) do
        local skill = Fk.skills[s]
        if skill.visible and not skill:hasTag(Skill.AttachedKingdom) and skill:isInstanceOf(TriggerSkill) then
          if skill.event == event_class or table.find(skill.related_skills, function (s2)
            return s2:isInstanceOf(TriggerSkill) and not s2.is_delay_effect and s2.event == event_class
          end) then
            table.insert(tag, s)
          end
        end
      end
    end
  end
  local skills = table.filter(tag, function (s)
    return not player:hasSkill(s, true)
  end)
  if #skills > 0 then
    room:handleAddLoseSkills(player, table.random(skills))
  end
end

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) or target ~= player then return false end
    for _, id in pairs(player:getTableMark("emo__pod-round")) do
      if table.contains(player:getCardIds("he"), id) then
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local pat = tostring(Exppattern{ id = player:getTableMark("emo__pod-round") })
    local cards = player.room:askToDiscard(player, {
      pattern = pat, min_num = 1, max_num = 1, skill_name = skel.name, include_equip = true, skip = true,
      prompt = "#emo__pod-cost",
    })
    if #cards > 0 then
      event:setCostData(self, cards[1] )
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cid = event:getCostData(self)
    room:throwCard(cid, skel.name, player, player)
    if player.dead then return end
    local t = Fk:getCardById(cid, true).type
    if t == Card.TypeEquip then
      local tos = room:askToChoosePlayers(player, {
        targets = room.alive_players, min_num = 1, max_num = 1, skill_name = skel.name,
        prompt = "#emo__pod-turnover",
      })
      if #tos > 0 then
        local to = tos[1]
        to:turnOver()
      end
    elseif t == Card.TypeTrick then
      getSkillFromEvent(player, "Damaged")
    else
      room:addPlayerMark(player, "@emo__pod_damage")
    end
  end,
})

skel:addEffect(fk.Damage, {
  is_delay_effect = true,
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return not player.dead and target == player and player:getMark("@emo__pod_damage") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = player:getMark("@emo__pod_damage")
    room:setPlayerMark(player, "@emo__pod_damage", 0)
    for i = 1, num do
      if player.dead then return end
      local tos = room:askToChoosePlayers(player, {
        targets = room.alive_players, min_num = 1, max_num = 1, skill_name = skel.name,
        prompt = "#emo__pod-damage",
      })
      if #tos > 0 then
        local to = tos[1]
        to:turnOver()
      else
        getSkillFromEvent(player, "Damage")
      end
    end
  end,
})

skel:addEffect(fk.RoundEnd, {
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    for _, id in pairs(player:getTableMark("emo__pod-round")) do
      if table.contains(player:getCardIds("he"), id) then
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getTableMark("emo__pod-round"), function (id)
      return table.contains(player:getCardIds("he"), id)
    end)
    room:throwCard(cards, skel.name, player, player)
    if player.dead then return end
    player:gainAnExtraTurn(true, skel.name)
  end,
})

return skel
